<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     <el-form-item label="年" prop="year">
       <el-select v-model="queryParams.year" placeholder="请选择年" clearable>
         <el-option
           v-for="dict in dict.type.sl_attendance_year"
           :key="dict.value"
           :label="dict.label"
           :value="dict.value"
         />
       </el-select>
     </el-form-item>
     <el-form-item label="月" prop="month">
       <el-select v-model="queryParams.month" placeholder="请选择月" clearable>
         <el-option
           v-for="dict in dict.type.sl_attendance_month"
           :key="dict.value"
           :label="dict.label"
           :value="dict.value"
         />
       </el-select>
     </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sl:sattendance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sl:sattendance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sl:sattendance:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sattendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年" align="center" prop="year" />
      <el-table-column label="月" align="center" prop="month" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="满勤天数" align="center" prop="fullAttendance" />
      <el-table-column label="高温补贴" align="center" prop="highTemperatureSubsidy" />
      <el-table-column label="开始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:sattendance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:sattendance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改特殊考勤天数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
       <el-form-item label="年" prop="year">
         <el-select v-model="form.year" placeholder="请选择年">
           <el-option
             v-for="dict in dict.type.sl_attendance_year"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           ></el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="月" prop="month">
         <el-select v-model="form.month" placeholder="请选择月">
           <el-option
             v-for="dict in dict.type.sl_attendance_month"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           ></el-option>
         </el-select>
       </el-form-item>
        <el-form-item label="部门" prop="deptId">
            <treeselect v-model="form.deptId" :options="deptOptions" :normalizer="normalizer" placeholder="选择部门" />
        </el-form-item>
        <el-form-item label="满勤天数" prop="fullAttendance">
          <el-input v-model="form.fullAttendance" class="def-input-number" type="number"  placeholder="请输入满勤天数" >
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="高温补贴" prop="highTemperatureSubsidy">
          <el-input v-model="form.highTemperatureSubsidy" class="def-input-number" type="number"  placeholder="请输入高温补贴" />
        </el-form-item>
        <el-form-item label="日期范围" prop="daterangeDate">
         <el-date-picker
            v-model="form.daterangeDate"
            type="daterange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
       <el-form-item label="显示顺序" prop="faSort">
         <el-input-number v-model="form.faSort" controls-position="right" :min="0" />
       </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSattendance, getSattendance, delSattendance, addSattendance, updateSattendance } from "@/api/sl/sattendance";
import { listDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Sattendance",
  dicts: ['sl_attendance_month', 'sl_attendance_year'],
  components: { Treeselect },
  data() {
    let watermarkContentVaild = (rule, value, callback) => {
      if (value == null || value === "") {
        callback();
      } else {
        let length = value.length
        if (length > 7) {
          callback(new Error("输入不能超过7位"))
        } else {
          callback();
        }
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特殊考勤天数表格数据
      sattendanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: null,
        month: null,
        deptId: null,
        faSort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          { required: true, message: "年不能为空", trigger: "blur" }
        ],
        month: [
          { required: true, message: "月不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门不能为空", trigger: "blur" }
        ],
        fullAttendance: [
          { required: true, message: "满勤天数不能为空", trigger: "blur" },
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
        startDate: [
          { required: true, message: "开始日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
        faSort: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ],
        highTemperatureSubsidy: [
          { required: true, message: "高温补贴不能为空", trigger: "blur" },
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
        daterangeDate: [
          { required: true, message: "日期范围不能为空", trigger: "blur" }
        ],
      },
      deptOptions: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    listDept().then(response => {
      this.deptOptions = this.handleTree(response.data, "deptId");
    });
  },
  methods: {
    /** 查询特殊考勤天数列表 */
    getList() {
      this.loading = true;
      listSattendance(this.queryParams).then(response => {
        this.sattendanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sattendanceId: null,
        year: null,
        month: null,
        deptId: null,
        deptName: null,
        fullAttendance: null,
        startDate: null,
        endDate: null,
        faSort: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        highTemperatureSubsidy: null,
        daterangeDate: [],
      };
      //this.daterangeDate = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sattendanceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加特殊考勤天数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sattendanceId = row.sattendanceId || this.ids
      getSattendance(sattendanceId).then(response => {
        this.form = response.data;
        if (null != this.form.startDate && '' != this.form.startDate && null != this.form.endDate && '' != this.form.endDate) {
          
          this.$set(this.form,'daterangeDate',[response.data.startDate,response.data.endDate]);
        }
        this.open = true;
        this.title = "修改特殊考勤天数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (null != this.form.daterangeDate &&  this.form.daterangeDate.length>0) {
            this.form.startDate = this.form.daterangeDate[0];
            this.form.endDate = this.form.daterangeDate[1];
          }
          if (this.form.sattendanceId != null) {
            updateSattendance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSattendance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sattendanceIds = row.sattendanceId || this.ids;
      this.$modal.confirm('是否确认删除特殊考勤天数编号为"' + sattendanceIds + '"的数据项？').then(function() {
        return delSattendance(sattendanceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/sattendance/export', {
        ...this.queryParams
      }, `sattendance_${new Date().getTime()}.xlsx`)
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // fnChangeRangeDate(value){
    //   this.$set(this.form,'daterangeDate',[value[0],value[1]]);
    //   //this.$set(this.form, 'daterangeDate', [value[0], value[1]])
    // }
  }
};
</script>
